// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using ValidationAspects.Sdk;

namespace ValidationAspects
{
	public class ValidationException : Exception
	{
		public ValidationException() {}

		public ValidationException(string message) : base(message) {}

		public ValidationException(string message, Exception innerException) : base(message, innerException) {}

		public IValidationContext Context { get; protected set; }

		public virtual IEnumerable<string> Messages
		{
			get { return new[] { Message }; }
		}

		public virtual void SetContext(IValidationContext context)
		{
			Enforce.IsNotNull(context, "context");
			Context = context;
		}

		public string GetValidationTargetName()
		{
			if (Context == null)
				return "Value";

			if (Context.Property != null)
				return Context.Property.Name;

			if (Context.Parameter != null)
				return string.Format("{0}.{1}.{2}", Context.Type.Name, Context.Method.Name, Context.Parameter.Name);

			return Context.Type.Name;
		}
	}
}